package com.ruoyi.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.app.domain.AppImComplaint;
import com.ruoyi.app.domain.bo.AppImComplaintBo;
import com.ruoyi.app.domain.vo.AppImComplaintVo;
import com.ruoyi.app.mapper.AppImComplaintMapper;
import com.ruoyi.app.service.IAppImComplaintService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 投诉Service业务层处理
 *
 * @author ruoyi
 * @date 2025-11-11
 */
@RequiredArgsConstructor
@Service
public class AppImComplaintServiceImpl implements IAppImComplaintService {

    private final AppImComplaintMapper baseMapper;

    /**
     * 查询投诉
     */
    @Override
    public AppImComplaintVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询投诉列表
     */
    @Override
    public TableDataInfo<AppImComplaintVo> queryPageList(AppImComplaintBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppImComplaint> lqw = buildQueryWrapper(bo);
        Page<AppImComplaintVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询投诉列表
     */
    @Override
    public List<AppImComplaintVo> queryList(AppImComplaintBo bo) {
        LambdaQueryWrapper<AppImComplaint> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppImComplaint> buildQueryWrapper(AppImComplaintBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppImComplaint> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getUserId()), AppImComplaint::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getTargetId()), AppImComplaint::getTargetId, bo.getTargetId());
        lqw.eq(bo.getComplaintType() != null, AppImComplaint::getComplaintType, bo.getComplaintType());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), AppImComplaint::getContent, bo.getContent());
        lqw.eq(bo.getStatus() != null, AppImComplaint::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getHandlerId()), AppImComplaint::getHandlerId, bo.getHandlerId());
        lqw.eq(StringUtils.isNotBlank(bo.getImage()), AppImComplaint::getImage, bo.getImage());
        return lqw;
    }

    /**
     * 新增投诉
     */
    @Override
    public Boolean insertByBo(AppImComplaintBo bo) {
        AppImComplaint add = BeanUtil.toBean(bo, AppImComplaint.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改投诉
     */
    @Override
    public Boolean updateByBo(AppImComplaintBo bo) {
        AppImComplaint update = BeanUtil.toBean(bo, AppImComplaint.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppImComplaint entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除投诉
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
